﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Collections.ObjectModel;
using UBCon.Resources;
using UBCon.Navigation.Models;

namespace UBCon.Navigation
{
    [StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(Gallery))]
    [TemplatePart(Name=FilterComboBoxName, Type=typeof(ComboBox))]
    public class GallerySet:ItemsControl
    {
        #region Constructors

        static GallerySet() 
        {
            Type ownerType = typeof(GallerySet);
            GallerySet.DefaultStyleKeyProperty.OverrideMetadata(ownerType, new FrameworkPropertyMetadata(ownerType));
        }

        public GallerySet() : base() 
        {
            //FilterItemsCollection = new ObservableCollection<FilterItemModel>();
        }

        #endregion

        #region Properties
        
        public bool Filterable
        {
            get { return (bool)GetValue(FilterableProperty); }
            set { SetValue(FilterableProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Filterable.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FilterableProperty =
            DependencyProperty.Register("Filterable", typeof(bool), typeof(GallerySet), 
            new FrameworkPropertyMetadata(false, new PropertyChangedCallback(GallerySet.OnFilterablePropertyChanged)));


        public ObservableCollection<FilterItemModel> FilterItemsCollection
        {
            get { return (ObservableCollection<Models.FilterItemModel>)GetValue(FilterItemsCollectionProperty); }
            set { SetValue(FilterItemsCollectionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for FilterItemsCollection.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty FilterItemsCollectionProperty =
            DependencyProperty.Register("FilterItemsCollection", typeof(ObservableCollection<FilterItemModel>), typeof(GallerySet), new FrameworkPropertyMetadata(null));

        internal ComboBox FilterComboBox 
        {
            get { return filterComboBox; }
            private set { filterComboBox = value; }
        }

        #endregion

        #region Depenency Property Changed Callbacks

        private static void OnFilterablePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e) 
        {
            GallerySet gallerySet = (GallerySet)sender;
            if (!gallerySet.IsLoaded) 
            {
                gallerySet.Loaded += gallerySet_Loaded;
            }
        }

        static void gallerySet_Loaded(object sender, RoutedEventArgs e)
        {
            GallerySet gallerySet = (GallerySet)sender;
            gallerySet.Loaded -= gallerySet_Loaded;
            foreach (object item in gallerySet.Items)
            {
                if (item is Gallery) 
                {
                    
                    Gallery gallery = item as Gallery;
                    if (gallerySet.CanBeAdded(gallery))
                    {
                        FilterItemModel model = new FilterItemModel();
                        model.Gallery = gallery;
                        model.Header = gallery.Header;
                        gallerySet.FilterItemsCollection.Add(model);
                    }
                }
            }
            if (gallerySet.FilterItemsCollection.Count > 1)
            {
                gallerySet.FilterItemsCollection.Insert(0, new FilterItemModel() { Header = LocalizationResources.AllText, IsAll = true });
            }
            if (gallerySet.FilterComboBox != null) 
            {
                gallerySet.FilterComboBox.SelectedIndex = 0;
            }
        }

        private bool CanBeAdded(Gallery gallery) 
        {
            return (from m in FilterItemsCollection where m.Gallery == gallery select m).ToList().Count == 0;
        }

        internal void OnFilterItemSelected(FilterItemModel model) 
        {
            if (FilterItemsCollection.Contains(model)) 
            {
                List<FilterItemModel> items = (from m in FilterItemsCollection where m != model select m).ToList();

                if (model.IsAll)
                {
                    foreach (FilterItemModel item in items)
                    {
                        if (item.Gallery != null) 
                        {
                            item.Gallery.Visibility = System.Windows.Visibility.Visible;
                        }
                        if (item.IsSelected) item.IsSelected = false;
                    }
                }
                else 
                {
                    foreach (FilterItemModel item in items)
                    {
                        item.IsSelected = false;
                    }
                }
            }
        }
        #endregion

        #region Overrides

        public override void OnApplyTemplate()
        {
            FilterComboBox = GetTemplateChild(FilterComboBoxName) as ComboBox;
            if (FilterComboBox != null) 
            {
                //FilterComboBox.IsFilterComboBox = true;
            }
            base.OnApplyTemplate();
        }

        protected override DependencyObject GetContainerForItemOverride()
        {
            return new Gallery();
        }

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return item is Gallery;
        }
        #endregion

        #region Private Utilities
        #endregion

        #region Event Handlers
        #endregion

        #region Fields
        private const string FilterComboBoxName = "PART_FilterComboBox";
        private ComboBox filterComboBox;
        #endregion

        #region Events
        #endregion
    }
}
